﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GoldPrinter;
using cfg = System.Configuration;
namespace BYManageInfoSystem
{
    public partial class Frm_TTRecvMoneyDetail : Form
    {
        private DGBY.BLL.FeeTTLoanAndFeeRegDetailInfoTab m_bllFeeTTLoanAndFeeRegDetailInfoTab = new DGBY.BLL.FeeTTLoanAndFeeRegDetailInfoTab();
        private int m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        //ExcelBase eB = new ExcelBase();
        public Frm_TTRecvMoneyDetail()
        {
            InitializeComponent();
        }

        private void ShowAllRecord(string strWhere)
        {

            dataGridView1.DataSource = null;
            DataSet ds = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetTTRecvMoneyDetailInfoList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.Columns[0].Visible = false;
            }

            for(int i = dataGridView1.Columns.Count; i > dataGridView1.Columns.Count- 8; i--)
            {
                dataGridView1.Columns[i - 1].Visible = false;
            }
            
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strFeeTTLoanAndFeeRegDetailInfoTabID = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID = Convert.ToInt32(strFeeTTLoanAndFeeRegDetailInfoTabID, 10);
            cellFeeTTLoanAndFeeRegDetailInfoTabClick();
        }
        private void cellFeeTTLoanAndFeeRegDetailInfoTabClick()
        {
            DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab modelFeeTTLoanAndFeeRegDetailInfoTab
                = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetModel(m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID);
            if (modelFeeTTLoanAndFeeRegDetailInfoTab != null)
            {
                this.textBoxBankNo.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.BankNo;
                this.textBoxInvoiceNo.Text = modelFeeTTLoanAndFeeRegDetailInfoTab.InvoiceNo;

            }

        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FeeTTLoanAndFeeRegDetailInfoTab modelFeeTTLoanAndFeeRegDetailInfoTab = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetModel(m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID);
                if (modelFeeTTLoanAndFeeRegDetailInfoTab == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }


                modelFeeTTLoanAndFeeRegDetailInfoTab.BankNo = this.textBoxBankNo.Text;
                modelFeeTTLoanAndFeeRegDetailInfoTab.InvoiceNo = this.textBoxInvoiceNo.Text;

                m_bllFeeTTLoanAndFeeRegDetailInfoTab.Update(modelFeeTTLoanAndFeeRegDetailInfoTab);
                ShowAllRecord("");
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonViewDetail_Click(object sender, EventArgs e)
        {
            Frm_TTFeeDetail frm_TTFeeDetail = new Frm_TTFeeDetail(m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID);
            frm_TTFeeDetail.StartPosition = FormStartPosition.CenterScreen;
            frm_TTFeeDetail.ShowDialog();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataSet ds = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetTTRecvMoneyDetailInfoList("FeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegDetailInfoTabID = '"
                                                                         + m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID.ToString() + "'");

            DataTable dt = ds.Tables[0];


            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "信用证实收费用明细表.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.FeeDetailSheet_DataTableToExcel(dt);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("请选择一条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonPrintPreview_Click(object sender, EventArgs e)
        {
            DataSet ds = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetTTRecvMoneyDetailInfoList("FeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegDetailInfoTabID = '"
                                                                         + m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID.ToString() + "'");

            DataTable dt = ds.Tables[0];
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "信用证实收费用明细表.xlt";
                    string outFile = "";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);
                    try
                    {
                        eB.FeeDetailSheet_DataTableToExcelAndPrintPreview(dt);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("请先关闭当前excel文档!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择一条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            DataSet ds = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetTTRecvMoneyDetailInfoList("FeeTTLoanAndFeeRegDetailInfoTab.FeeTTLoanAndFeeRegDetailInfoTabID = '"
                                                                        + m_nSelectFeeTTLoanAndFeeRegDetailInfoTabID.ToString() + "'");

            DataTable dt = ds.Tables[0];


            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {

                    string outFile = "";



                    string templetFile = templetFilePath + "信用证实收费用明细表.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.FeeDetailSheet_DataTableToExcelAndPrint(dt);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("请选择一条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Frm_TTRecvMoneyDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            //eB.Close();
        }
    }
}
